package com.seagox.chat.chat.verify;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seagox.chat.chat.entity.BaseUser;
import com.seagox.chat.chat.entity.Depart;
import com.seagox.chat.chat.mapper.DepartMapper;
import com.seagox.chat.chat.mapper.UserMapper;
import com.seagox.chat.template.UserModel;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;

/**
 * 自定义验证
 */
@Component
public class UserExcelVerifyHandler implements IExcelVerifyHandler<UserModel> {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private DepartMapper departMapper;


	/**
	 * ExcelVerifyHandlerResult suceess :代表验证成功还是失败 msg:失败的原因
	 */
	@Override
	public ExcelVerifyHandlerResult verifyHandler(UserModel obj) {
		ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);

		LambdaQueryWrapper<BaseUser> qwAccount = new LambdaQueryWrapper<>();
		qwAccount.eq(BaseUser::getPhone, obj.getPhone()).in(BaseUser::getStatus, Arrays.asList(1,2));
		Long userCount = userMapper.selectCount(qwAccount);
		if (userCount != 0) {
			result.setSuccess(false);
			result.setMsg("手机号已存在");
		}
		LambdaQueryWrapper<Depart> qwDepart = new LambdaQueryWrapper<>();
		qwDepart.eq(Depart::getCode, obj.getCode());
		Long departCount = departMapper.selectCount(qwDepart);
		if (departCount == 0) {
			result.setSuccess(false);
			result.setMsg("部门编码不存在");
		}		
		return result;
	}

}
